package com.jrx.anytxn.reporting.job.unionFile.config;

import com.jrx.anytxn.reporting.config.AnyTxnBatchProperties;
import com.jrx.anytxn.reporting.constant.ReportingConstant;
import com.jrx.anytxn.reporting.job.unionFile.step.ContractFileProcessor;
import com.jrx.anytxn.reporting.job.unionFile.step.ContractFileWriter;
import com.jrx.anytxn.reporting.job.unionFile.step.LoanInfoFileReader;
import com.jrx.anytxn.reporting.job.unionFile.step.UnionContractFileListener;
import com.jrx.anytxn.transaction.entity.AmLoanAccountInfo;
import org.apache.ibatis.session.SqlSessionFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.batch.core.Job;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.batch.core.configuration.annotation.JobBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.core.configuration.annotation.StepScope;
import org.springframework.batch.core.launch.support.RunIdIncrementer;
import org.springframework.batch.item.ItemReader;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.task.SimpleAsyncTaskExecutor;

import javax.annotation.Resource;

/**
 * 生成联盟合同文件(全量)
 * lbz
 */
@Configuration
@EnableConfigurationProperties(AnyTxnBatchProperties.class)
@EnableBatchProcessing
public class GenerateContractFileConfiguration {

    private static final Logger logger = LoggerFactory.getLogger(GenerateLoanInfoFileConfiguration.class);

    @Resource(name = "bizSqlSessionFactory")
    SqlSessionFactory sqlSessionFactory;

    @Autowired
    public JobBuilderFactory jobBuilderFactory;
    @Autowired
    public StepBuilderFactory stepBuilderFactory;
    @Autowired
    private AnyTxnBatchProperties anyTxnBatchProperties;
    @Autowired
    private UnionContractFileListener unionContractFileListener;
    @Autowired
    private ContractFileProcessor contractFileProcessor;
    @Autowired
    private ContractFileWriter contractFileWriter;

    @Bean
    public Job generateContractFileJob() {
        return jobBuilderFactory.get("generateContractFileJob").incrementer(new RunIdIncrementer()).listener(unionContractFileListener)
                .start(generateContractFileStep())
                .build();
    }

    @Bean
    public Step generateContractFileStep(){
        return stepBuilderFactory.get("generateContractFileStep")
                .<AmLoanAccountInfo, String>chunk(anyTxnBatchProperties.getFileConfig().getChunkLimit())
                //表示一批处理100
                // 条记录，数据是逐条发送给processor处理的，处理100条记录后，统一返回给writer进行保存处理
                .reader(contractFileReader())
                .processor(contractFileProcessor)
                .writer(contractFileWriter)
                .taskExecutor(new SimpleAsyncTaskExecutor())
                .throttleLimit(anyTxnBatchProperties.getFileConfig().getThrottleLimit())//多线程*/
                .build();
    }

    @Bean
    @StepScope
    public ItemReader<AmLoanAccountInfo> contractFileReader(){
        logger.info("联盟合同信息表批量,机构号：{}", ReportingConstant.DEFAULT_ORG_NUMBER);

        return new LoanInfoFileReader(sqlSessionFactory,anyTxnBatchProperties.getFileConfig().getPageSize());
    }
}
